<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Region extends Model
{
    protected $fillable = ['name','parent_id','map','longitude','latitude','domain','alias','is_hot','letter','seo_title','seo_keywords','seo_description','level','sort','status'];

    //子分类
    public function childs()
    {
        return $this->hasMany('App\Models\Region','parent_id','id');
    }

    //所有子类
    public function allChilds()
    {
        return $this->childs()->with('allChilds');
    }

    public function property()
    {
        return $this->hasMany('App\Models\Property','area','id');
    }

    public function handroom()
    {
        return $this->hasMany('App\Models\Handroom','area','id');
    }

    public function rentalhouse()
    {
        return $this->hasMany('App\Models\Rentalhouse','area','id');
    }

    public function metro()
    {
        return $this->hasMany('App\Models\Metro');
    }

    public function house_price()
    {
        return $this->hasMany('App\Models\HousePrice');
    }

    //    递归获取区域id
    static public function getIds($id)
    {
        $menus = self::with(['allChilds' => function ($query) {
            $query->select('id','parent_id');
        }])->where('id',$id)->get(['id']);
        $arr = [$menus[0]['id']];
        foreach ($menus[0]['allChilds'] as $v){
            $arr[] = $v['id'];
        }
        return $arr;
    }
}
